package com.tjbaobao.videoplayol.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.Bundle;

import com.tjbaobao.framework.entity.ui.TitleBarInfo;
import com.tjbaobao.framework.tjbase.TJActivity;
import com.tjbaobao.framework.ui.BaseTitleBar;
import com.tjbaobao.framework.utils.ConstantUtil;
import com.tjbaobao.framework.utils.ImageUtil;
import com.tjbaobao.framework.utils.Tools;
import com.tjbaobao.videoplayer.listener.OnTJVideoPlayerListener;
import com.tjbaobao.videoplayol.R;
import com.tjbaobao.videoplayol.base.AppActivity;
import com.tjbaobao.videoplayol.modal.info.VideoCacheInfo;
import com.tjbaobao.videoplayol.ui.ButtonProgressView;
import com.tjbaobao.videoplayol.ui.ImgChooseView;
import com.tjbaobao.videoplayol.ui.TJVideoPlayerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.paperdb.Paper;

/**
 *
 * Created by TJbaobao on 2018/1/18.
 */

public class VideoPreviewActivity extends AppActivity{

    @BindView(R.id.tjVideoView)
    TJVideoPlayerView tjVideoView ;

    @BindView(R.id.imgChooseView)
    ImgChooseView mImgChooseView ;

    @BindView(R.id.bt_next)
    ButtonProgressView mButtonProgressView ;

    private MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();

    private String videoPath ;
    private VideoCacheInfo videoCacheInfo ;

    @Override
    protected void onInitValues(Bundle bundle) {
        videoCacheInfo = (VideoCacheInfo) this.getIntent().getSerializableExtra("videoCacheInfo");
        videoPath = videoCacheInfo.getVideoPath();
        Paper.book("app").write("videoCacheInfo",videoCacheInfo);
    }

    @Override
    protected void onInitView() {
        setContentView(R.layout.video_preview_activity_layout);
        ButterKnife.bind(this);
        mButtonProgressView.setText(getStringById(R.string.video_preview_utiling));
        mButtonProgressView.setClickable(false);
        mButtonProgressView.setMBackgroundColor(Color.parseColor("#d9d9d9"));

        tjVideoView.setOnTJVideoPlayerListener(new MyOnTJVideoPlayerListener());
        tjVideoView.setVideoPath(videoPath);
        metadataRetriever.setDataSource(videoPath);

    }

    @Override
    protected void onInitTitleBar(BaseTitleBar baseTitleBar) {
        baseTitleBar.setTitle(getStringById(R.string.video_preview_title));
        baseTitleBar.addImageToLeft(R.drawable.fw_ic_back);
    }

    @Override
    protected void onLoadData() {

    }

    @Override
    public <V extends TitleBarInfo.BaseView> void onTitleBarClick(int layoutType, int position, V info) {
        if(layoutType==BaseTitleBar.LAYOUT_LEFT&&position==0)
        {
            this.finish();
        }
    }

    @OnClick(R.id.bt_next)
    public void onNext()
    {
        ImgChooseView.ImgChooseInfo chooseInfo = null;
        List<ImgChooseView.ImgChooseInfo> imgInfoList = mImgChooseView.values();
        for(ImgChooseView.ImgChooseInfo info : imgInfoList)
        {
            if(info.isSelect)
            {
                chooseInfo = info;
                break ;
            }
        }
        if (chooseInfo!=null)
        {
            String imgPath = ConstantUtil.getImageCachePath()+ "imgVideo.jpg";
            ImageUtil.saveBitmap(chooseInfo.getBitmap(),imgPath);
            Intent intent = new Intent(this,VideoConfigActivity.class);
            intent.putExtra("videoCacheInfo",videoCacheInfo);
            intent.putExtra("imgVideoPath",imgPath);
            startActivity(intent);
        }
        else
        {
            Tools.showToast("至少选择一在张封面");
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * 获取某张截图
     */
    private void getPreviewImageByPosition(long longTime,final int position) {
        int f = (int) (longTime / 10);
        int d = (int) ((longTime - 2 * f) / 6);
        int startTime = f + position * d;
        final Bitmap bitmap = getPreviewBitmapByTime(startTime);
        VideoPreviewActivity.this.runOnUiThread(() ->
                {
                    mImgChooseView.addInfo(bitmap);
                    if(position==5)
                    {
                        mButtonProgressView.setClickable(true);
                        mButtonProgressView.setText(getStringById(R.string.video_preview_next));
                        mButtonProgressView.setMBackgroundColor(context.getResources().getColor(R.color.app_red_off));
                    }
                }
        );
    }

    /**
     * 获取某一毫秒的截图
     * @param startTime
     * @return
     */
    private Bitmap getPreviewBitmapByTime(int startTime)
    {
        if(videoPath.contains(".mp4")||videoPath.contains(".MP4"))
        {
            Bitmap bitmap = metadataRetriever.getFrameAtTime(startTime*1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            return bitmap;
        }
        else
        {
            Bitmap bitmap = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
            bitmap.eraseColor(Color.GRAY);
            return bitmap;
        }
    }

    private class MyOnTJVideoPlayerListener extends OnTJVideoPlayerListener
    {
        @Override
        public void onPlay() {

        }

        @Override
        public void onPause() {

        }

        @Override
        public void onPrepared(MediaPlayer mp) {
            final int longTime = mp.getDuration();
            getPreviewImage(longTime);
        }
    }

    private boolean isGetPVImg = false;
    private void getPreviewImage(final int longTime)
    {
        if(!isGetPVImg)
        {
            isGetPVImg = true;
            new Thread(() -> {
                for (int i = 0; i < 6; i++)
                {
                    getPreviewImageByPosition(longTime,i);
                }
            }).start();
        }
    }

}
